home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / nihcl-30.lha / nihcl-3.0 / vector / IntVec.h < prev    next >
C/C++ Source or Header  |  1990-05-16  |  9KB  |  282 lines

  1. #ifndef    INTVEC_H
  2. #define    INTVEC_H
  3.  
  4. /* IntVec.h -- Integer Vectors
  5.  
  6.     THIS SOFTWARE FITS THE DESCRIPTION IN THE U.S. COPYRIGHT ACT OF A
  7.     "UNITED STATES GOVERNMENT WORK".  IT WAS WRITTEN AS A PART OF THE
  8.     AUTHOR'S OFFICIAL DUTIES AS A GOVERNMENT EMPLOYEE.  THIS MEANS IT
  9.     CANNOT BE COPYRIGHTED.  THIS SOFTWARE IS FREELY AVAILABLE TO THE
  10.     PUBLIC FOR USE WITHOUT A COPYRIGHT NOTICE, AND THERE ARE NO
  11.     RESTRICTIONS ON ITS USE, NOW OR SUBSEQUENTLY.
  12.  
  13. Author:
  14.     K. E. Gorlen
  15.     Bg. 12A, Rm. 2033
  16.     Computer Systems Laboratory
  17.     Division of Computer Research and Technology
  18.     National Institutes of Health
  19.     Bethesda, Maryland 20892
  20.     Phone: (301) 496-1111
  21.     uucp: uunet!nih-csl!kgorlen
  22.     Internet:kgorlen@alw.nih.gov
  23.  
  24. Modification History:
  25.  
  26. $Log:    IntVec.h,v $
  27.  * Revision 3.0  90/05/16  23:00:32  kgorlen
  28.  * Release for 1st edition.
  29.  * 
  30. */
  31. #include "Vector.h"
  32. #include "BitVec.h"
  33.  
  34. class IntSlice;
  35. class IntPick;
  36. class IntSlct;
  37.  
  38. class IntVec : public Vector {
  39.     DECLARE_MEMBERS(IntVec);
  40.     int* v;        // pointer to data, NULL if empty vector
  41.     void indexRangeErr() const;
  42. protected:
  43.     virtual void storer(OIOofd&) const;
  44.     virtual void storer(OIOout&) const;
  45. public:
  46.     IntVec(unsigned len =0);
  47.     IntVec(unsigned len, int from, int by =1);
  48.     IntVec(const int*, unsigned len);
  49.     IntVec(const IntVec&);
  50.     IntVec(const IntSlice&);
  51.     ~IntVec()            { delete v; }
  52.     IntSlice operator()(int pos, unsigned lgt, int stride =1);
  53.     const IntSlice operator()(int pos, unsigned lgt, int stride =1) const;
  54.     int* pt()        { return v; }
  55.     const int* pt() const    { return v; }
  56.     operator IntSlice();
  57.     operator const IntSlice() const;
  58.     operator DoubleVec();
  59. //    operator LongVec();
  60.     int& operator[](int i) {    // vector element
  61.         if ((unsigned)i >= n) indexRangeErr();
  62.         return v[i];
  63.     }
  64.     const int& operator[](int i) const {    // vector element
  65.         if ((unsigned)i >= n) indexRangeErr();
  66.         return v[i];
  67.     }
  68.     int& operator()(int i)            { return v[i]; }
  69.     const int& operator()(int i) const  { return v[i]; }
  70.     IntPick operator[](const IntVec&);
  71.     const IntPick operator[](const IntVec&) const;
  72.     IntSlct operator[](const BitVec&);
  73.     const IntSlct operator[](const BitVec&) const;
  74.     void /*IntVec::*/operator=(const IntVec&);
  75.     void /*IntVec::*/operator=(const IntSlice&);
  76.     void /*IntVec::*/operator=(const IntSlct&);
  77.     void /*IntVec::*/operator=(const IntPick&);
  78.     void /*IntVec::*/operator=(int);
  79.     void /*IntVec::*/lengthErr(const IntSlice&) const;
  80.     void selectErr(const BitVec&) const;
  81.     virtual void deepenShallowCopy();
  82.     virtual unsigned hash() const;
  83.     virtual bool isEqual(const Object&) const;
  84.     virtual void printOn(ostream& strm =cout) const;
  85.     virtual void scanFrom(istream& strm);
  86.     virtual void sort();
  87.     virtual const Class* species() const;
  88. };
  89.  
  90. inline unsigned BitPick::length() const { return X->length(); }
  91.     
  92. class TempIntVec : public IntVec {
  93.     friend IntSlice;
  94.     friend IntPick;
  95.     friend IntSlct;
  96.     TempIntVec(unsigned len =0) : IntVec(len) {}
  97.     virtual void free();
  98. };
  99.  
  100. class IntSlice : public NIHCL {
  101.     IntVec* V;    // vector pointer
  102.     int* p;        // slice pointer
  103.     unsigned l;    // slice length
  104.     int k;        // slice stride
  105.     IntSlice(const IntVec& v, int pos, unsigned lgt, int stride =1);
  106.     IntSlice(const IntVec& v, unsigned lgt) {
  107.         V = &(IntVec&)v;  p = ((IntVec&)v).pt();  l = lgt;  k = 1;
  108.     }
  109.     IntSlice(const IntSlice&);
  110.     friend IntVec;
  111. public:
  112.     IntSlice(const IntPick&);
  113.     IntSlice(const IntSlct&);
  114.     ~IntSlice()        { V->free(); }
  115.     int* pt()        { return p; }
  116.     const int* pt() const    { return p; }
  117.     unsigned length() const    { return l; }
  118.     int stride() const    { return k; }
  119.     void /*IntSlice::*/operator=(const IntVec&);
  120.     void /*IntSlice::*/operator=(const IntPick&);
  121.     void /*IntSlice::*/operator=(const IntSlct&);
  122.     void /*IntSlice::*/operator=(const IntSlice&);
  123.     void /*IntSlice::*/operator=(int);
  124.     void /*IntSlice::*/lengthErr(const IntVec&) const;
  125.     void /*IntSlice::*/lengthErr(const IntSlice&) const;
  126.     void selectErr(const BitVec&) const;
  127. friend    IntVec    operator-(const IntSlice&);
  128. friend    IntVec    operator!(const IntSlice&);
  129. friend    IntVec    operator~(const IntSlice&);
  130. friend    IntVec    operator++(IntSlice&);
  131. friend    IntVec    operator--(IntSlice&);
  132. friend    IntVec    operator*(const IntSlice&,const IntSlice&);
  133. friend    IntVec    operator/(const IntSlice&,const IntSlice&);
  134. friend    IntVec    operator%(const IntSlice&,const IntSlice&);
  135. friend    IntVec    operator+(const IntSlice&,const IntSlice&);
  136. friend    IntVec    operator-(const IntSlice&,const IntSlice&);
  137. friend    IntVec    operator&(const IntSlice&,const IntSlice&);
  138. friend    IntVec    operator^(const IntSlice&,const IntSlice&);
  139. friend    IntVec    operator|(const IntSlice&,const IntSlice&);
  140. friend    IntVec    operator*(const IntSlice&,int);
  141. friend    IntVec    operator*(int s,const IntSlice& V)  { return V*s; }
  142. friend    IntVec    operator/(const IntSlice&,int);
  143. friend    IntVec    operator/(int,const IntSlice&);
  144. friend    IntVec    operator%(const IntSlice&,int);
  145. friend    IntVec    operator%(int,const IntSlice&);
  146. friend    IntVec    operator+(const IntSlice&,int);
  147. friend    IntVec    operator+(int s,const IntSlice& V)  { return V+s; }
  148. friend    IntVec    operator-(const IntSlice&,int);
  149. friend    IntVec    operator-(int,const IntSlice&);
  150. friend    IntVec    operator&(const IntSlice&,int);
  151. friend    IntVec    operator&(int s,const IntSlice& V)  { return V&s; }
  152. friend    IntVec    operator^(const IntSlice&,int);
  153. friend    IntVec    operator^(int s,const IntSlice& V)  { return V^s; }
  154. friend    IntVec    operator|(const IntSlice&,int);
  155. friend    IntVec    operator|(int s,const IntSlice& V)  { return V|s; }
  156. friend    BitVec    operator<(const IntSlice&,const IntSlice&);
  157. friend    BitVec    operator>(const IntSlice& U,const IntSlice& V)    { return V < U; }
  158. friend    BitVec    operator<=(const IntSlice&,const IntSlice&);
  159. friend    BitVec    operator>=(const IntSlice& U,const IntSlice& V) { return V <= U; }
  160. friend    BitVec    operator==(const IntSlice&,const IntSlice&);
  161. friend    BitVec    operator!=(const IntSlice&,const IntSlice& V);
  162. friend    BitVec    operator<(const IntSlice&,int);
  163. friend    BitVec    operator<(int s,const IntSlice& V)  { return V > s; }
  164. friend    BitVec    operator>(const IntSlice&,int);
  165. friend    BitVec    operator>(int s,const IntSlice& V)  { return V < s; }
  166. friend    BitVec    operator<=(const IntSlice&,int);
  167. friend    BitVec    operator<=(int s,const IntSlice& V) { return V >= s; }
  168. friend    BitVec    operator>=(const IntSlice&,int);
  169. friend    BitVec    operator>=(int s,const IntSlice& V) { return V <= s; }
  170. friend    BitVec    operator==(const IntSlice&,int);
  171. friend    BitVec    operator==(int s,const IntSlice& V) { return V == s; }
  172. friend    BitVec    operator!=(const IntSlice&,int);
  173. friend    BitVec    operator!=(int s,const IntSlice& V) { return V != s; }
  174. friend    void    operator+=(IntSlice&,const IntSlice&);
  175. friend    void    operator+=(IntSlice&,int);
  176. friend    void    operator-=(IntSlice&,const IntSlice&);
  177. friend    void    operator-=(IntSlice&,int);
  178. friend    void    operator*=(IntSlice&,const IntSlice&);
  179. friend    void    operator*=(IntSlice&,int);
  180. friend    void    operator/=(IntSlice&,const IntSlice&);
  181. friend    void    operator/=(IntSlice&,int);
  182. friend    void    operator%=(IntSlice&,const IntSlice&);
  183. friend    void    operator%=(IntSlice&,int);
  184. friend    void    operator&=(IntSlice&,const IntSlice&);
  185. friend    void    operator&=(IntSlice&,int);
  186. friend    void    operator^=(IntSlice&,const IntSlice&);
  187. friend    void    operator^=(IntSlice&,int);
  188. friend    void    operator|=(IntSlice&,const IntSlice&);
  189. friend    void    operator|=(IntSlice&,int);
  190. friend    IntVec    abs(const IntSlice& V);
  191. friend    IntVec    cumsum(const IntSlice&);
  192. friend    IntVec    delta(const IntSlice&);
  193. friend    int    dot(const IntSlice&,const IntSlice&);
  194. friend    int    max(const IntSlice&);
  195. friend    int    min(const IntSlice&);
  196. friend    int    prod(const IntSlice&);
  197. friend    IntVec    reverse(const IntSlice&);
  198. friend    int    sum(const IntSlice&);
  199. };
  200.  
  201. class IntPick : public NIHCL {
  202.     IntVec* V;
  203.     const IntVec* X;
  204.     IntPick(const IntVec& v,const IntVec& x)    { V = &(IntVec&)v;  X = &x; }
  205.     IntPick(const IntPick& s)            { V = s.V; X = s.X; }
  206.     friend IntVec;
  207.     friend IntSlice;
  208.     friend IntSlct;
  209. public:
  210.     void /*IntPick::*/operator=(const IntVec&);
  211.     void /*IntPick::*/operator=(const IntPick&);
  212.     void /*IntPick::*/operator=(const IntSlct&);
  213.     void /*IntPick::*/operator=(const IntSlice&);
  214.     void /*IntPick::*/operator=(int);
  215.     unsigned length() const    { return X->length(); }
  216. };
  217.  
  218. class IntSlct : public NIHCL {
  219.     IntVec* V;
  220.     const BitVec* B;
  221.     IntSlct(const IntVec& v, const BitVec& b)   { V = &(IntVec&)v;  B = &b; }
  222.     IntSlct(const IntSlct& s)            { V = s.V; B = s.B; }
  223.     friend IntVec;
  224.     friend IntSlice;
  225.     friend IntPick;
  226. public:
  227.     void /*IntSlct::*/operator=(const IntVec&);
  228.     void /*IntSlct::*/operator=(const IntPick&);
  229.     void /*IntSlct::*/operator=(const IntSlct&);
  230.     void /*IntSlct::*/operator=(const IntSlice&);
  231.     void /*IntSlct::*/operator=(int);
  232.     unsigned length() const    { return B->length(); }
  233. };
  234.  
  235. inline IntSlice IntVec::operator()(int pos, unsigned lgt, int stride)
  236. {
  237.     IntSlice s(*this,pos,lgt,stride);
  238.     return s;
  239. }
  240.  
  241. inline const IntSlice IntVec::operator()(int pos, unsigned lgt, int stride) const
  242. {
  243.     const IntSlice s(*this,pos,lgt,stride);
  244.     return s;
  245. }
  246.  
  247. inline IntVec::operator IntSlice()
  248. {
  249.     IntSlice s(*this,length());
  250.     return s;
  251. }
  252.  
  253. inline IntVec::operator const IntSlice() const
  254. {
  255.     const IntSlice s(*this,length());
  256.     return s;
  257. }
  258.  
  259. inline IntPick IntVec::operator[](const IntVec& I)
  260. {
  261.     return IntPick(*this,I);
  262. }
  263.  
  264. inline const IntPick IntVec::operator[](const IntVec& I) const
  265. {
  266.     const IntPick t(*this,I);
  267.     return t;
  268. }
  269.  
  270. inline IntSlct IntVec::operator[](const BitVec& B)
  271. {
  272.     return IntSlct(*this,B);
  273. }
  274.  
  275. inline const IntSlct IntVec::operator[](const BitVec& B) const
  276. {
  277.     const IntSlct t(*this,B);
  278.     return t;
  279. }
  280.  
  281. #endif
  282.